//
//  NewtabBar.swift
//  ZBWb
//
//  Created by 天隐翎 on 16/4/6.
//  Copyright © 2016年 csdn. All rights reserved.
//

import UIKit

class NewtabBar: UITabBar {
//    重写父类方法
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(newbut)
    }
    required init?(coder aDecoder: NSCoder) {
        
//        默认实现报错
        fatalError("init(coder:) has not been implemented")
    }


// 重写layoutSubView.获取tabbarButton的frame.把中间的位置空出来
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let btnW = self.frame.width/5
        let btnH = self.bounds.height
//        按钮的大小
        let btn = CGRectMake(0, 0, btnW, btnH)
        
        var index: CGFloat = 0
        
//        遍历子视图
        for subView in subviews{
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!){
                subView.frame = CGRectOffset(btn, btnW * index, 0)
                
                index += (index == 1) ? 2:1
                
            }
        }
//        设置好已懒加载的按钮的frame
        newbut.frame = CGRectOffset(btn, 2*btnW, 0)
    }
//    懒加载创建tabbarbutton
    lazy var newbut: UIButton = {
        let btn = UIButton()
//        设置图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
//        设置背图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)

        return btn
        
    }()
    
    

}
